package ch.ffhs.bscinf2009.organizeanything.gui;

import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import ch.ffhs.bscinf2009.organizeanything.Environment;
import ch.ffhs.bscinf2009.organizeanything.R;
import ch.ffhs.bscinf2009.organizeanything.data.AbstractType;
import ch.ffhs.bscinf2009.organizeanything.data.DateType;
import ch.ffhs.bscinf2009.organizeanything.data.Organizable;
import ch.ffhs.bscinf2009.organizeanything.data.ValueChooserType;

/**
 * @author lir
 * Manages an existing instance
 */
public class OrganizableManageObject extends Activity {
	TableLayout tb;
	int counter = 1;
	private Organizable detail;
	
	private Button dateButtonPressed = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.organizable_manage_object);

		detail = (Organizable) getIntent().getExtras()
				.getSerializable("detail");

		TextView name = (TextView) findViewById(R.id.manage_object_name);
		name.setText(detail.getOrganizableName());

		tb = (TableLayout) findViewById(R.id.table_manage_object);

		for (AbstractType t : detail.getProperties()) {
			if (t instanceof ValueChooserType) {
				ValueChooserType vt = (ValueChooserType) t;
				addChooserProperty(t.getPropertyName(),
						((ValueChooserType) t).getPossibleData(), vt.displayValue());
			} else if (t instanceof DateType) {
				addDateProperty(t.getPropertyName(), t.displayValue());
			} else {
				addProperty(t.getPropertyName(), t.displayValue());
			}
		}

	}
	
	private void addDateProperty(String propertyName, String value) {
		TextView et1 = new TextView(this);
		et1.setText(propertyName);
		final Button et2 = new Button(this);
		et2.setText(value);
		et2.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				dateButtonPressed = et2;
				showDialog(666);
			}
		});

		TableRow tr = new TableRow(getBaseContext());
		tr.addView(et1);
		tr.addView(et2);
		tb.addView(tr);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		if(id==666){
			DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
				public void onDateSet(DatePicker view, int selectedYear,
						int selectedMonth, int selectedDay) {
					GregorianCalendar cal = new GregorianCalendar(selectedYear, selectedMonth, selectedDay);
					dateButtonPressed.setText(DateType.df.format(cal.getTime()));
					dateButtonPressed = null;
				}
			};
			Date d = null;
			try {
				d = DateType.df.parse(dateButtonPressed.getText().toString());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			GregorianCalendar cal = new GregorianCalendar();
			if(d!=null){
				cal.setTime(d);
			}
			DatePickerDialog datePickerDialog = new DatePickerDialog(OrganizableManageObject.this, datePickerListener, cal.get(GregorianCalendar.YEAR),cal.get(GregorianCalendar.MONTH),cal.get(GregorianCalendar.DAY_OF_MONTH));
			return datePickerDialog;
		}
		return super.onCreateDialog(id);
	}

	private void addChooserProperty(String propertyName,
			List<String> possibleData, String defaultValue) {
		Spinner spin = new Spinner(this);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, possibleData);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(dataAdapter);
		spin.setSelection(possibleData.indexOf(defaultValue));
		
		TextView et1 = new TextView(this);
		et1.setText(propertyName);
		
		TableRow tr = new TableRow(getBaseContext());
		tr.addView(et1);
		tr.addView(spin);
		tb.addView(tr);
	}

	private void addProperty(String name, String value) {
		TextView et1 = new TextView(this);
		et1.setText(name);
		EditText et2 = new EditText(this);
		et2.setText(value);
		TableRow tr = new TableRow(getBaseContext());
		tr.addView(et1);
		tr.addView(et2);
		tb.addView(tr);
	}

	/**
	 * Stores the changed values of the instance in the database
	 * @param view the view the method was called from
	 */
	public void update(View view) {
		int i = 1;
		for (AbstractType t : detail.getProperties()) {
			TableRow row = (TableRow) tb.getChildAt(i);
			if (t instanceof ValueChooserType) {
				ValueChooserType vt = (ValueChooserType) t;
				vt.setData(((Spinner) row.getChildAt(1)).getSelectedItem().toString());
			} else if (t instanceof DateType) {
				DateType vt = (DateType) t;
				try {
					vt.setData(DateType.df.parse(((Button) row.getChildAt(1)).getText().toString()));
				} catch (ParseException e) {
					vt.setData(new Date(0));
				}
			}else {
				t.setData(((EditText) row.getChildAt(1)).getText().toString());
			}
			i++;
		}
		
		Environment.getInstance().getDB(this).updateEntry(detail);
		Intent resultIntent = new Intent();
		resultIntent.putExtra("udpateOrganizableObject", detail);
		setResult(Activity.RESULT_OK, resultIntent);
		finish();

	}

}